import { Injectable } from '@angular/core';
import { Hero } from './hero'
import { HEROES } from './mock-heros'
import { Observable, of } from 'rxjs';
import { MessageService } from './message.service'

@Injectable({
  providedIn: 'root'
})
export class HeroService {

  constructor(private messageService: MessageService) { }
  getHero(): Observable<Hero[]> {
    this.messageService.add('HeroService: fetched heroes  message from "message service"')
    return of(HEROES)
  }
  getHeroOne(id: number): Observable<Hero> {
    // const targetHero = HEROES.filter(hero => hero.id === id)
    // console.log(targetHero, 'aaaaaaaaaaaa');

    // return of(targetHero)
    let hero: Hero
    HEROES.forEach(item => {
      if (item.id === id) {
        hero = item
      }
    })
    return of(hero)
  }
}
